package test.concurrency.art;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * User: zhanglin
 * Date: 2016/3/23
 * Time: 22:18
 */
public class Counter {
    private AtomicInteger atomicI = new AtomicInteger(0);
    private volatile int i = 0;

    public static void main(String[] args) {
        final Counter cas = new Counter();
        List<Thread> ts = new ArrayList<Thread>(120);
        long start = System.currentTimeMillis();
        for (int j = 0; j < 100; j++) {
            Thread t = new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int i = 0; i < 10000; i++) {
                        cas.count();
                        //cas.safeCount();
                    }
                }
            });
            ts.add(t);
        }

        for (Thread t : ts) {
            t.start();
        }
        // 等待所有线程执行完成
        for (Thread t : ts) {
            try {
                t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        System.out.println(cas.i);
        System.out.println(cas.atomicI.get());
        System.out.println(System.currentTimeMillis() - start);

    }

    /**
     * 使用CAS实现线程安全计数器
     */
    private void safeCount() {
        //for (; ; ) {
        //    int i = atomicI.get();
        //    boolean suc = atomicI.compareAndSet(i, ++i);
        //    if (suc) {
        //        break;
        //    }
        //}

        atomicI.incrementAndGet();
    }

    /**
     * 非线程安全计数器
     */
    private void count() {
        i++;
    }
}